#light
namespace FSharp.WebTools.ClientControls

open System
open System.Web
open System.Web.UI

open FSharp.WebTools
open FSharp.WebTools.Core
open FSharp.WebTools.ClientControls
open FSharp.WebTools.ClientLibrary

[<MixedSide>]
type ClockControl = class
  inherit ClientControl as base
  interface INamingContainer

  val mutable lblOutput : Label;

  [<WebBehavior(WebBehavior.ClientSide)>]
  val mutable timer : ClientLibrary.Timer;
  
  new() = { timer = (Array.zero_create 1).[0]; lblOutput = (Array.zero_create 1).[0]; }
  
  override this.OnInit (e:System.EventArgs) =
    this.lblOutput <- new Label();
    this.lblOutput.ID <- "lblOutput";
    (this.lblOutput :> WebControls.Label).Text <- DateTime.Now.ToString("hh:mm:ss");
    this.Controls.Add(this.lblOutput);
    base.OnInit(e);

  [<WebBehavior(WebBehavior.ClientSide); ReflectedDefinition>]
  member this.Client_Load ((sender:Object),(e:ClientLibrary.EventArgs)) =
    let t = new ClientLibrary.Timer(); 
    this.timer <- t;
    this.timer.Interval <- 200;
    this.timer.Elapsed.AddClient(this.timerTick); 
    this.timer.Start();
    
  [<WebBehavior(WebBehavior.ClientSide); ReflectedDefinition>]
  member this.timerTick ((sender:Object),(e:ClientLibrary.EventArgs)) =
    client
      { let dt = DateTime.Now;
        let s = dt.ToString("hh:mm:ss");
        do! this.lblOutput.set_ClientText(s); }
  
  override this.Render(wr) =
    wr.Write("<span id=\"{0}\">", this.ClientID);
    base.Render(wr);
    wr.Write("</span>");
    
end